<%@page import="utils.IdGenerator"%>
<%@page import="sun.util.calendar.BaseCalendar.Date"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" import="logic.*, show.*, java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SOS PLANET - Ayuda al mundo</title>
<link rel="stylesheet" type="text/css" href="css/style.css" /><style type="text/css">
.tabletirso{
	text-alig: center;
	margin: 30px;
	width: 100%;
}
table {
	background-color: #FFFFFF;
	border: none;
	color: #495E49;
	font-size: 14px;
	text-align: left;
	margin: 0 auto;
	width: 400px;
}

table a:link, table a:visited, table a:active {
	background-color: transparent;
	color: #0096BD;
	text-decoration: none;
}

table a:hover {
	background: transparent;
	color: #000000;
	text-decoration: underline;
}

table caption {
	background-color: transparent;
	color: #67BD2A;
	font-family: Georgia, serif;
	font-size: 24px;
	padding-bottom: 12px;
	padding-left: 8px;
	text-align: left;
}

table, td, th {
	margin: 0;
	padding: 0;
}

tbody td, tbody th {
	background-color: #D6F2C3;
	border-bottom: 2px solid #B3DE94;
	border-top: 3px solid #FFFFFF;
	padding: 9px;
}

tbody tr.odd th, tbody tr.odd td {
	background-color: #C8EDAE;
	border-bottom: 2px solid #67BD2A;
}

tbody tr:hover td, tbody tr:hover th {
	background-color: #BAE899;
}

td, th {
	vertical-align: middle;
}

tfoot td, tfoot th {
	font-weight: bold;
	padding: 4px 8px 6px 9px;
}

thead th {
	font-family: arial, helvetica, sans-serif;
	font-size: 14px;
	font-weight: bold;
	line-height: 19px;
	padding: 0 8px 2px 8px;
	white-space: nowrap;
}</style>
</head>
<body>
<div id="wrapper">
<div class="content">
<div id="header">
	<%@ include file = "includes/header.jsp" %>
</div><!-- header -->

<div id="menu">      
	<%@ include file = "includes/menu.html" %>
</div><!-- menu -->
           

<%
	String idProduct = (String)request.getParameter("pid");
	User u = (User)session.getAttribute("session.userAux");
	
	if(u == null ){
		%><a href="FrontController?res=userDenied.jsp"></a><%
	}
	
	
	//String idUser = u.selectIDUserByNick(u.getName());
	ShoppingKart sessionKart = null;
	sessionKart = (ShoppingKart)session.getAttribute("session.kart");
	
	if(idProduct != null && idProduct.length() > 0){
		
		
		
		if(sessionKart == null){
			sessionKart = new ShoppingKart();
			List<String> lp = new ArrayList<String>();
			lp.add(idProduct);
			String id = IdGenerator.getInstance().getKey();
			sessionKart.setId(id);
			sessionKart.setIdUser(u.getId());
			sessionKart.setProductList(lp);
			sessionKart.setTotalPrice(ProductList.getInstance().getProduct(idProduct).getPrice());
			sessionKart.setTotalProducts(1);
		}else{
			List<String> ls = sessionKart.getProductList();
			int aux = 0;
			boolean b = false;
			while(aux < ls.size() && b == false){
				if(ls.get(aux).equals(idProduct)){
					b = true;
				}
			}
			if(b == true){
				%><div id="parrafoQ1">No puedes añadir dos veces el mismo producto </div>
				<% 
			}else{
				Float totalPrice = sessionKart.getTotalPrice();
				totalPrice += ProductList.getInstance().getProduct(idProduct).getPrice();
				sessionKart.setTotalPrice(totalPrice);
				sessionKart.getProductList().add(idProduct);
			
				Integer totalProd = sessionKart.getTotalProducts() + 1;
				sessionKart.setTotalProducts(totalProd);
			}
		}
		
		//Put ShoppingKart into session
		session.setAttribute("session.kart", sessionKart);
	}
		%>
		<div class="tabletirso">
		<table><caption>Carrito de la compra</caption>
		<thead>
		<tr>
		<th>Nombre</th>
		<th>Precio</th>
		</tr>
		</thead>
		<tbody>
		<% 
		if(sessionKart != null){
		for(int i = 0; i < sessionKart.getProductList().size(); i++){
			Product p1 = ProductList.getInstance().getProduct(sessionKart.getProductList().get(i));
			String name = p1.getName();
			Float price = p1.getPrice();
			%>
		
		<tr>
		<td><%=name %></td>
		<td><%=price %></td>
		</tr>
		<%	
		}%>
		</tbody>
		<tfoot>
		<tr></tr><tr></tr>
		<tr>
		<td>Precio total</td>
		<td></td>
		<td><%=sessionKart.getTotalPrice() %></td>
		</tr>
		<tr>
		<td>Numero de productos</td>
		<td></td>
		<td><%=sessionKart.getTotalProducts() %></td>
		</tr>
		</tfoot>
		</table>
		<%}else{
			%><div id="parrafoQ1">No existe el carrito</div><% 
		}%>
		</div>
		<p id="parrafoQ3">
		<a href="FrontController?res=products.jsp"><input id="parrafoQ2" type="submit" name="Submit" value="Seguir comprando"></a>
		<a href="FrontController?res=questionDestiny.jsp"><input id="parrafoQ2" type="submit" name="Submit" value="Confirmar carrito"></a></p>
<div id="footer">
	<%@ include file = "includes/footer.html" %>
</div><!--footer-->

</div><!--content-->
</div><!--wrapper-->
</body>
</html>